Ext.namespace('VAS');

VAS.AdvancedSearchWindow = Ext.extend(Ext.Window,{
	manager:wgroup,
	initComponent : function() {
		var _this = this;
		var this_fieldSet = {};
		this_fieldSet.maxCols = 1;
	    this_fieldSet.items =[{
	        "tablecode": "t_user",
	        "orderFlag": false,
	        "iconCombo": false,
	        "loadForce": false,
	        "fieldLabel": "搜索条件名", 
	        "version": 0,
	        "id": "advancedSearch_Name",
	        "maxLength": 50,
	        "maxLengthText":"最大长度为50个字符",
	        "name": "name",
	        "xtype": "textfield",
	        "orderSc": "0",
	        "minChars": 0,
	        "allowBlank": false,
	        "blankText": "必填项",
	        "width": "377",
	        "url": null,
	        "staticData": [],
	        "datadecimals": 0,
	        "viewName": "name",
	        "readOnly": false,
	        "hidden": false,
	        "orderid": 0,
	        "staticFlag": true
	    }];
	    this_fieldSet.items.push({
	    	"tablecode": "t_user",
	        "orderFlag": false,
	        "iconCombo": false,
	        "loadForce": false,
	        "fieldLabel": "备注",
	        "version": 0,
	        "id": "advancedSearch_Tips",
	        "maxLength": 100,
	        "maxLengthText":"最大长度为100个字符",
	        "name": "tips",
	        "xtype": "textfield",
	        "orderSc": "0",
	        "minChars": 0,
	        "allowBlank": true,
	        "width": "0",
	        "url": null,
	        "staticData": [],
	        "datadecimals": 0,
	        "viewName": "tips",
	        "readOnly": false,
	        "hidden": false,
	        "orderid": 0,
	        "staticFlag": true
	    });
	    for( i = 0; i<_this.fieldSet.length; i++ ){
	    	if( _this.fieldSet[i].xtype == 'textfield'){
	    		_this.fieldSet[i].name = 'gridFilter_'+_this.fieldSet[i].name+'_like';
	    	}else if(_this.fieldSet[i].xtype == 'dynamicRadioGroup' || _this.fieldSet[i].xtype == 'dynamicCombo'){
	    		_this.fieldSet[i].name = 'gridFilter_'+_this.fieldSet[i].name+'_eq';
	    	}else {
	    		_this.fieldSet[i].name = 'gridFilter_'+_this.fieldSet[i].name;
	    	}
	    	if( _this.fieldSet[i].xtype == 'dynamicCombo'){
	    		_this.fieldSet[i].url = _this.fieldSet[i].url + '&entryMenuId='+_this.menuid;
	    	}
	    	this_fieldSet.items.push(_this.fieldSet[i]);
	    };

	    this_fieldSet.items.push({
            "tablecode": "t_user",
            "orderFlag": false,
            "iconCombo": false,
            "loadForce": false,
            "fieldLabel": "主键",
            "version": 0,
            "id": "advancedSearch_Id",
            "maxLength": 50,
            "name": "id",
            "xtype": "hidden",
            "orderSc": "0",
            "minChars": 0,
            "allowBlank": true,
            "width": "0",
            "url": null,
            "staticData": [],
            "datadecimals": 0,
            "viewName": "id",
            "readOnly": false,
            "hidden": false,
            "orderid": 0,
            "staticFlag": true
        });
        this_fieldSet.items.push({
            "tablecode": "t_user",
            "orderFlag": false,
            "iconCombo": false,
            "loadForce": false,
            "fieldLabel": "version",
            "version": 0,
            "id": "advancedSearch_version",
            "maxLength": 50,
            "maxLengthText":"最大长度为50个字符",
            "name": "version",
            "xtype": "hidden",
            "orderSc": "0",
            "minChars": 0,
            "allowBlank": true,
            "width": "0",
            "url": null,
            "staticData": [],
            "datadecimals": 0,
            "viewName": "version",
            "readOnly": false,
            "hidden": false,
            "orderid": 0,
            "staticFlag": true
        });
    	var  _fieldSet = {
			"columnWidth"   : "",
			"labelWidth"    : "",
			"title"         : "",
			"items"         : []
		};
		if(typeof(this_fieldSet)==='object'){
	   		_fieldSet={};
	    	_fieldSet=this_fieldSet;
		};
		
	 	//Here is where we create the Form
	    _this.ridPanel = new VAS.AdvancedSearchGridPanel({
			checkboxSelModel  : true,
			autoScroll: 	true,
			autoWidth: true,
			page 			  : _this.wins.gridPanel.page == undefined || typeof(_this.wins.gridPanel.page) != "boolean" ? true : _this.wins.gridPanel.page,
            height: 500,
            title:'搜索条件',
            border: true,
            menuid			   : _this.menuid,
            gridPanelId		   : _this.gridPanelId
	    });
	    

	    var saveAndClose = new Ext.Button({
	    	text    : '保存并关闭',
	    	handler : function() {
	    		var version = _this.formPanel.form.findField("version")
						.getValue();
				if ("" == version) {
					var advancedSearch_Name = _this.formPanel.form
							.findField("advancedSearch_Name").getValue();
					var grid = _this.ridPanel;
					var store = grid.store;
					var rowCount = store.getCount(); // 记录数
					var cm = grid.getColumnModel();
					var colCount = cm.getColumnCount(); // 列数
					for (i = 0; i < rowCount; i++) {
						var record = store.getAt(i);
						if (record.data.name == advancedSearch_Name) {
							Ext.Msg.alert('提示', '此搜索条件名已存在！');
							return;
						}
					}
				}
	    		
				if (_this.formPanel.form.isValid()) {
					var _postData = _this.formPanel.form.getValues();
					_postData.viewpanelid = _this.gridPanelId;
					_postData.entryMenuId = _this.menuid;
                        
					Ext.Ajax.request({
						url    : 'admin/view/cfg!saveOrUpdateViewPanelFilter.action',
						method : "POST",
						params : _postData,
						success: function(response) {
						    //检查session是否过期
						    checkTimeOut(response);
							var responseObj = Ext.util.JSON.decode(response.responseText);
							if (responseObj.success) {
								//高级搜索条件有更新时，高级搜索comboBox需要reload
								_this.advancedSearchComboBox.store.reload();
								 _this.ridPanel.store.reload();
								 if(responseObj.msg == '' ||  responseObj.msg == undefined){
								 	responseObj.msg = '成功';
								 }
						   		Ext.Msg.alert('提示', responseObj.msg,function(bt){
							   		_this.close();
						   		});
							} else {
								alertFailure(responseObj);
							}
						}, 
						failure: function(response) {
							Ext.Msg.alert('提示','服务器异常！');
						}
			   		})
				}
	    	}
		});
		
		var reset = new Ext.Button({
			text:'重置',
        	handler : function() {
        		_this.formPanel.getForm().reset();
        	}
		});
		
		var close = new Ext.Button({
			text:'关闭',
        	handler : function() {
        		_this.close();
        	}
		});
		
		_this.formPanel = new Ext.ux.DynamicItems({
	    	id		 : 'advancedSearch_formPanel',
	    	autoScroll:true,
	    	frame	: true,
	    	layout   : 'column',
	    	title	 : '搜索条件设置',
			fieldSet : _fieldSet
			,width	 : 457
			,height  : 500
			,buttons  : [saveAndClose,reset,close]
	    });		
		
		thisConfig = {
			manager:wgroup,
			items: [{
				layout:'column',
				items:[{
						layout      : 'form',
						columnWidth : 0.3,
						items		: _this.ridPanel
					}
					,{
						layout      : 'form',
						columnWidth : 0.7,
						items		: _this.formPanel
					}
				]
			}]
		};
		
		Ext.apply(this, thisConfig);
		Ext.apply(this.initialConfig, thisConfig);
		VAS.AdvancedSearchWindow.superclass.initComponent.apply(this, arguments);
	}
	,onRender : function(ct,position){
		var _this=this;
		VAS.AdvancedSearchWindow.superclass.onRender.call(this, ct, position);	
	    for( i = 0; i<_this.fieldSet.length; i++ ){
	    	if( _this.fieldSet[i].xtype == 'dynamicCombo' && _this.fieldSet[i].staticFlag == false){
	    		Ext.getCmp( _this.fieldSet[i].id).store.load();
	    	}
	    };
	}
	
});
Ext.reg('advancedSearch-win',VAS.AdvancedSearchWindow);
